package com.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.c.entity.DoctorWorkNumber;
import com.c.entity.TodayInformation;
import com.c.entity.TongJi;
import com.c.entity.TotalInformation;
import com.c.mapper.*;
import com.c.service.ReportService;
import com.c.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 运营数据统计服务
 */
@Service
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private TodayInformationMapper todayInformationMapper;
    @Autowired
    private TotalInformationMapper totalInformationMapper;

    @Autowired
    private DoctorWorkNumberMapper doctorWorkNumberMapper;

    //查询运营数据
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员数
        Integer todayNewMember = memberMapper.findMemberCountByDate(today);
        System.out.println("-----------------------today:"+today);
        //总会员数
        Integer totalMember = memberMapper.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberMapper.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberMapper.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderMapper.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderMapper.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderMapper.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderMapper.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderMapper.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderMapper.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐查询
        List<Map> hotSetmeal = orderMapper.findHotSetmeal();

        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);
        return result;
    }

    @Override
    public Map getAnalyzeNumber() throws Exception {
        QueryWrapper<TodayInformation> wrapper=new QueryWrapper<>();
        String date2String = DateUtils.parseDate2String(new Date());
        wrapper.eq("todaydate",date2String);
        TodayInformation todayInformation = todayInformationMapper.selectOne(wrapper);
        List<TotalInformation> totalInformations = totalInformationMapper.selectList(null);
        List<TongJi> tongJis=new ArrayList<>();
        if(Objects.nonNull(todayInformation)){
            TongJi tongJi0=new TongJi("今日预约人数",todayInformation.getTodayordernumber());
            TongJi tongJi1=new TongJi("今日评估人数",todayInformation.getTodayestimatenumber());
            TongJi tongJi2=new TongJi("今日干预人数",todayInformation.getTodayintervenenumber());
            TongJi tongJi3=new TongJi("今日评论人数",todayInformation.getTodaycommentnumber());
            tongJis.add(tongJi0);
            tongJis.add(tongJi1);
            tongJis.add(tongJi2);
            tongJis.add(tongJi3);
        }


        List<TongJi> tongJis2=new ArrayList<>();
        if(!CollectionUtils.isEmpty(totalInformations)){
            TongJi tongJi02=new TongJi("总预约人数",totalInformations.get(0).getTotalordernumber());
            TongJi tongJi12=new TongJi("总评估人数",totalInformations.get(0).getTotalestimatenumber());
            TongJi tongJi22=new TongJi("总干预人数",totalInformations.get(0).getTotalintervenenumber());
            TongJi tongJi32=new TongJi("总评论人数",totalInformations.get(0).getTotalcommentnumber());
            tongJis2.add(tongJi02);
            tongJis2.add(tongJi12);
            tongJis2.add(tongJi22);
            tongJis2.add(tongJi32);
        }
        Map map=new HashMap();
        map.put("currentday",tongJis);
        map.put("gather",tongJis2);
        return map;
    }

    @Override
    public Map AnalyzeDoctorWorkNumber() {
        List<DoctorWorkNumber> doctorWorkNumbers = doctorWorkNumberMapper.selectList(null);
        List<DoctorWorkNumber> doctorWorkNumbers1 = doctorWorkNumbers.stream().sorted(Comparator.comparing(DoctorWorkNumber::getDoctorId)).collect(Collectors.toList());
        List<String> doctorNameList=new ArrayList<>();
        List<Integer> orderNumberList=new ArrayList<>();
        List<Integer> visitNumberList=new ArrayList<>();

        for (DoctorWorkNumber doctorWorkNumber:doctorWorkNumbers1){
            doctorNameList.add(doctorWorkNumber.getDoctorName());
            orderNumberList.add(doctorWorkNumber.getOrderNumber());
            visitNumberList.add(doctorWorkNumber.getVisitNumber());
        }
        Map map=new HashMap();
        map.put("doctorNameList",doctorNameList);
        map.put("orderNumberList",orderNumberList);
        map.put("visitNumberList",visitNumberList);

        return map;
    }
}
